package cn.yzgy.yzgyoa.common.handler;

import cn.yzgy.yzgyoa.common.crsp.Urmf;
import cn.yzgy.yzgyoa.common.entity.SysAlarm;
import cn.yzgy.yzgyoa.common.exception.SystemException;
import cn.yzgy.yzgyoa.common.service.impl.SysAlarmServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局SystemException处理
 *
 * @author Yqsen
 * @version 0.0.1
 * @since 2019-8-16 08:53:29
 */
@Slf4j
@Order(0)
@ControllerAdvice
@RestControllerAdvice
public class SystemExceptionHandler {

    @Autowired
    private SysAlarmServiceImpl sysAlarmService;

    @ResponseBody
    @ExceptionHandler(SystemException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Urmf systemExceptionHandler(SystemException e, HttpServletRequest request) {
        log.error(e.getAttachStr(), e);
        log.error("SYSTEM_EXCEPTION_HANDLER: Path:[{}] StatusCode:[{}]  StackTrace:[{}] AttachStr:[{}]",
                request.getRequestURI(),e.getStatusCode(), e.getStackTrace()[0], e.getAttachStr());

        sysAlarmService.saveExceptions(e);
        return Urmf.fail(request.getRequestURI(), e.getStatusCode(), e.getAttachStr());
    }

}
